package ch.fhnw.qualiscan.application.views;

import java.util.ArrayList;
import java.util.List;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.VerticalLayout;

import ch.fhnw.qualiscan.ApplicationData;
import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.controllers.Evaluation;
import ch.fhnw.qualiscan.application.elements.AnswersLegendBox;
import ch.fhnw.qualiscan.application.elements.EvaluationOptionsBox;
import ch.fhnw.qualiscan.core.View;
import ch.fhnw.qualiscan.core.db.pojos.Role;
import ch.fhnw.qualiscan.core.db.pojos.RoleText;
import eu.livotov.tpt.TPTApplication;
import eu.livotov.tpt.i18n.TM;

public class EvaluationView extends View {
	
	private Evaluation controller;
	
	HorizontalLayout wrapper = new HorizontalLayout();
	VerticalLayout content = new VerticalLayout();
	HorizontalLayout rolePicker;
	public VerticalLayout charts = new VerticalLayout();
	
	Label title;
	OptionGroup rolesSelect = null;
	Label rolesSelectCaption;
	
	
	public EvaluationView(Evaluation controller) {
		this.controller = controller;
	}
	
	@Override
	public void initComponents() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void buildLayout() {
		// build wrapper
		
		wrapper.setWidth("100%");
		wrapper.setSpacing(true);
				
		
		content.setSpacing(true);
		content.setWidth("100%");
				
		
		charts.setSpacing(true);

		wrapper.addComponent(content);
		addComponent(wrapper);		// TODO Auto-generated method stub
		
	}
	
	
	
	/**
	 * Builds the role picker widget in the top
	 * @return layout component
	 */
	public HorizontalLayout rolePicker() {
		rolePicker = new HorizontalLayout();
		rolePicker.setSpacing(true);
		rolePicker.setStyleName("rolePicker");
		rolePicker.setMargin(true);
		rolePicker.setCaption(TM.get("evaluation.filter"));
		
		rolesSelectCaption = textHelper.span(TM.get("evaluation.rolepicker.caption"));
		rolesSelectCaption.setWidth("100px");
		
		VerticalLayout roleList = new VerticalLayout();
		roleList.setSpacing(true);
		
		ArrayList<RoleText> roleTexts = controller.getRoleTexts();
		
		// build index
		List<Integer> answers = new ArrayList<Integer>();
		answers.add(0);
		for(RoleText o : roleTexts) {
			answers.add(o.getRole().getId());
		}

		rolesSelect = new OptionGroup(null, answers);
		rolesSelect.setWidth("190px");
		rolesSelect.setItemCaption(0, TM.get("evaluation.rolepicker.all"));
		for (RoleText o : roleTexts) {
			rolesSelect.setItemCaption(o.getRole().getId(), o.getTitle());
		}
		rolesSelect.select(0);
		
		rolesSelect.setImmediate(true);
		rolesSelect.addListener(new Property.ValueChangeListener() {
			public void valueChange(ValueChangeEvent event) {
				Integer roleId = Integer.parseInt(event.getProperty().getValue().toString());
				changeFilter(roleId);
			}
		});
		
		rolePicker.addComponent(rolesSelectCaption);
		rolePicker.addComponent(rolesSelect);
	
		return rolePicker;
	}
	
	/**
	 * This function changes the filter parameter in the url.
	 */
	public void changeFilter(int roleId) {
		
		// update checkbox
		
		controller.getAppData().getLoaderLib().setUrlFragment(2, ""+roleId+"");
		if(rolesSelect != null)
			rolesSelect.select((Object) controller.currentRole);
	}
	
	
	public void populatePage() {
		content.removeAllComponents();
		
		title = textHelper.h1(TM.get("evaluation.title", controller.currentSurvey.getTitle()));
		Label subTitle = textHelper.h2(TM.get("evaluation.domains"));
		
		// design workarounds
		HorizontalLayout spacer1 = new HorizontalLayout();
		spacer1.setHeight("35px");
		
		HorizontalLayout spacer2 = new HorizontalLayout();
		spacer2.setHeight("15px");
		
		// title
		content.addComponent(title);
		if(controller.currentSurvey.getStage().getId() == Config.STAGE_SURVEY) {
			HorizontalLayout warningWrap = new HorizontalLayout();
			warningWrap.setMargin(true);
			warningWrap.setSpacing(true);
			Label surveyOpen = textHelper.p(TM.get("warning.surveyopen"));
			warningWrap.addComponent(surveyOpen);
			warningWrap.setStyleName("box-warning");
			content.addComponent(warningWrap);
		}
		content.addComponent(spacer2);
		
		// header
		HorizontalLayout header = new HorizontalLayout();
		HorizontalLayout rolePicker = rolePicker();
		VerticalLayout legend = new AnswersLegendBox();
		VerticalLayout options = new EvaluationOptionsBox(controller.sur);
		
		header.addComponent(rolePicker);
		header.addComponent(options);
		header.addComponent(legend);
		
		content.addComponent(header);
		
		// charts
		content.addComponent(spacer1);
		
		content.addComponent(charts);
	}
	
	/**
	 * this function unloads the charts
	 */
	public void unload() {
		charts.removeAllComponents();
	}

	@Override
	public void reBuild() {
		// TODO Auto-generated method stub
		
	}

}
